import UIKit
import SnapKit

class UTBootPageViewController: UIViewController {
    
    var delegate: UTLaunchDelegate?
    
    lazy var pageResources: [UTBootPageModel] = {
        var array: [UTBootPageModel] = Array()
        let onePage: UTBootPageModel = UTBootPageModel()
        onePage.icon = "pic_1"
        let twoPage: UTBootPageModel = UTBootPageModel()
        twoPage.icon = "pic_2"
        let threePage: UTBootPageModel = UTBootPageModel()
        threePage.icon = "pic_3"
        let fourPage: UTBootPageModel = UTBootPageModel()
        fourPage.icon = "pic_4"
        array = [onePage, twoPage, threePage, fourPage]
        return array
    }()
    
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.delegate = self;
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        scrollView.backgroundColor = .white
        scrollView.contentInsetAdjustmentBehavior = .never
        return scrollView
    }()
    
    lazy var skipButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "bootpage_skip_icon"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        button.addTarget(self, action: #selector(goSkip(_:)), for: .touchUpInside)
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
    }
    
    func setupViews() {
        view.addSubview(self.scrollView)
        self.scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        for (index, value) in pageResources.enumerated() {
            let model: UTBootPageModel = value
            let imageView: UIImageView = UIImageView(image: UIImage.init(named: model.icon!))
            imageView.isUserInteractionEnabled = true
            let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(goNext(_:)))
            imageView.addGestureRecognizer(tap)
            self.scrollView.addSubview(imageView)
            imageView.snp.makeConstraints { make in
                make.top.equalToSuperview()
                make.left.equalTo(CGFloat(index) * UIScreen.main.bounds.size.width)
                make.width.equalTo(UIScreen.main.bounds.size.width)
                make.height.equalTo(UIScreen.main.bounds.size.height)
            }
            if index + 1 == pageResources.count {
                self.scrollView.snp.makeConstraints { make in
                    make.right.equalTo(imageView.snp.right)
                }
            }
        }
        view.addSubview(skipButton)
        skipButton.snp.makeConstraints { make in
            make.top.equalTo(UIApplication.shared.statusBarFrame.size.height + 8)
            make.right.equalTo(-17)
            make.width.equalTo(57)
            make.height.equalTo(26)
        }
    }
    
    @objc func goNext(_ sender: UITapGestureRecognizer) {
        let offsetX = scrollView.contentOffset.x + UIScreen.main.bounds.size.width
        if (offsetX <= UIScreen.main.bounds.size.width * CGFloat(pageResources.count)) {
            scrollView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)
            return
        }
        if (self.delegate != nil) {
            self.delegate?.dismiss()
        }
    }
    
    @objc func goSkip(_ sender: UIButton) {
        if (self.delegate != nil) {
            self.delegate?.dismiss()
        }
    }

}

extension UTBootPageViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetX = scrollView.contentOffset.x + UIScreen.main.bounds.size.width
        if (offsetX <= UIScreen.main.bounds.size.width * CGFloat(pageResources.count)) {
            return
        }
        if (self.delegate != nil) {
            self.delegate?.dismiss()
        }
    }
}



